package com.teziyou.commodity.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teziyou.commodity.bean.Page;
import com.teziyou.commodity.logic.AreaLogic;
import com.teziyou.commodity.logic.TicketLogic;

@RequestMapping("/distributor")
@Controller
public class DistributorController extends BaseController {
	
	@Autowired
	private AreaLogic areaLogic;
	@Autowired
	private TicketLogic ticketLogic;
	
	@RequestMapping("/add")
	public String add(ModelMap model) {
		model.addAttribute("provinces", areaLogic.getAdministrativeListByPid(1001, -1));
		return "distributor/add";
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public String save(HttpServletRequest req) {
		Map<String, String> map = super.getParaMap(req);
		try {
			ticketLogic.addDistributor(map);
			return success("保存成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			return fail("保存失败！！");
		}
	}
	
	@RequestMapping("/dialog")
	public String dialog(HttpServletRequest req) {
		Page distributorPage = ticketLogic.findDistributors(super.getParaMap(req));
		req.setAttribute("provinces", areaLogic.getAdministrativeListByPid(1001, -1));
		req.setAttribute("distributorPage", distributorPage);
		return "distributor/dialog";
	}
	
	@RequestMapping("/page")
	public String page(HttpServletRequest req) {
		Page distributorPage = ticketLogic.findDistributors(super.getParaMap(req));
		req.setAttribute("distributorPage", distributorPage);
		return "distributor/dialog_table";
	}
	
	@RequestMapping("/list")
	public String list(ModelMap model) {
		model.addAttribute("provinces", areaLogic.getAdministrativeListByPid(1001, -1));
		Page distributorPage = ticketLogic.findDistributors(new HashMap<String, String>());
		model.addAttribute("distributorPage", distributorPage);
		return "distributor/list";
	}
	
	@RequestMapping("/table")
	public String table(HttpServletRequest req) {
		Page distributorPage = ticketLogic.findDistributors(super.getParaMap(req));
		req.setAttribute("distributorPage", distributorPage);
		return "distributor/table";
	}

}
